package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.MyCompanyService;
import com.woniu.p2p.entity.CompanyMincompany;
import com.woniu.p2p.entity.InvestCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mycompany")
public class MyCompanyController {

    @Autowired
    MyCompanyService myCompanyService;

    @RequestMapping("/getmycompanydetail")
    public String getmycompany(Map map, String companystatus,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                               String companykeyword,
                               Integer pagenum,
                               Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        pagesize=pagesize==null?3:pagesize;
        PageInfo<InvestCompany> pageInfo = myCompanyService.findAllInvestCompany(pagenum, pagesize, companystatus, predate, nextdate, companykeyword);
        map.put("companydetails",pageInfo);
        map.put("companystatus",companystatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("companykeyword",companykeyword);
        map.put("pagesize",pagesize);

        List<InvestCompany> list = myCompanyService.selectAll();
        ArrayList<Integer> ids = new ArrayList<>();
        for(InvestCompany investCompany :list){
            ids.add(investCompany.getCompanyId());
        }
        map.put("ids",ids);
        return "content/company";
    }


    /**
     * 导出
     */
    @RequestMapping("/companyderive")
    public void exportRepository(int[] id,HttpServletResponse response) throws IOException {
        System.out.println(id);
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");


        List<InvestCompany> list = myCompanyService.findAllInvestCompany(id);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, InvestCompany.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }



    @RequestMapping("/getcompanydetail")
    @ResponseBody
    public List<CompanyMincompany> getcompanydetail(int id){
        List<CompanyMincompany> allMinCompany = myCompanyService.findAllMinCompany(id);
        return allMinCompany;
    }

    @RequestMapping("/insertCompany")
    public int insertCompany(String companyName,@DateTimeFormat(pattern = "yyyy-MM-dd") Date companyTime,
                             String companyAddress,String companyType,String companyManagestatus,String companyMoneystatus){
        InvestCompany investCompany = new InvestCompany(companyName, companyTime, companyAddress, companyType, companyManagestatus, companyMoneystatus);
        myCompanyService.insertCompany(investCompany);
        return 1;
    }



    @RequestMapping("/delonecompany")
    public int delonecompany(int id){
        myCompanyService.deleteCompanyById(id);
        return 1;
    }


    @RequestMapping("/delallcompany")
    public int delallcompany(int[] id){
        for (int i = 0; i <id.length ; i++) {
            myCompanyService.deleteCompanyById(id[i]);
        }
        return 1;
    }

}
